﻿Imports System
Imports System.DirectoryServices
Imports System.Security.Principal
Imports System.Workflow.Activities.Configuration

Namespace System.Workflow.Activities
    Public Class ActiveDirectoryRoleFactory
        ' Methods
        Shared Sub New()
            If (ActiveDirectoryRoleFactory.s_configuration Is Nothing) Then
                ActiveDirectoryRoleFactory.s_configuration = New ActiveDirectoryRoleFactoryConfiguration
            End If
        End Sub

        Public Shared Function CreateFromAlias(ByVal [alias] As String) As ActiveDirectoryRole
            If ([alias] Is Nothing) Then
                Throw New ArgumentNullException("alias")
            End If
            Dim adRole As New ActiveDirectoryRole(ActiveDirectoryRoleFactory.GetRootEntry, New DirectoryRootQuery("sAMAccountName", [alias], DirectoryQueryOperation.Equal))
            adRole.Operations.Add(ActiveDirectoryRoleFactory.s_directoryGroupQuery)
            ActiveDirectoryRoleFactory.ValidateRole(adRole)
            Return adRole
        End Function

        Public Shared Function CreateFromEmailAddress(ByVal emailAddress As String) As ActiveDirectoryRole
            If (emailAddress Is Nothing) Then
                Throw New ArgumentNullException("emailAddress")
            End If
            Dim adRole As New ActiveDirectoryRole(ActiveDirectoryRoleFactory.GetRootEntry, New DirectoryRootQuery("mail", emailAddress, DirectoryQueryOperation.Equal))
            adRole.Operations.Add(ActiveDirectoryRoleFactory.s_directoryGroupQuery)
            ActiveDirectoryRoleFactory.ValidateRole(adRole)
            Return adRole
        End Function

        Public Shared Function CreateFromSecurityIdentifier(ByVal sid As SecurityIdentifier) As ActiveDirectoryRole
            If (sid Is Nothing) Then
                Throw New ArgumentNullException("sid")
            End If
            Dim adRole As New ActiveDirectoryRole(ActiveDirectoryRoleFactory.GetRootEntry, New DirectoryRootQuery("objectSID", sid.ToString, DirectoryQueryOperation.Equal))
            adRole.Operations.Add(ActiveDirectoryRoleFactory.s_directoryGroupQuery)
            ActiveDirectoryRoleFactory.ValidateRole(adRole)
            Return adRole
        End Function

        Private Shared Function GetRootEntry() As DirectoryEntry
            If (ActiveDirectoryRoleFactory.s_rootEntry Is Nothing) Then
                If (((ActiveDirectoryRoleFactory.s_configuration Is Nothing) OrElse (ActiveDirectoryRoleFactory.s_configuration.RootPath Is Nothing)) OrElse (ActiveDirectoryRoleFactory.s_configuration.RootPath.Length = 0)) Then
                    ActiveDirectoryRoleFactory.s_rootEntry = New DirectoryEntry
                Else
                    ActiveDirectoryRoleFactory.s_rootEntry = New DirectoryEntry(ActiveDirectoryRoleFactory.s_configuration.RootPath)
                End If
            End If
            Return ActiveDirectoryRoleFactory.s_rootEntry
        End Function

        Private Shared Sub ValidateRole(ByVal adRole As ActiveDirectoryRole)
            If (adRole.GetEntries.Count = 0) Then
                Throw New ArgumentException(SR.GetString("Error_NoMatchingActiveDirectoryEntry"))
            End If
        End Sub


        ' Properties
        Public Shared ReadOnly Property Configuration As ActiveDirectoryRoleFactoryConfiguration
            Get
                Return ActiveDirectoryRoleFactory.s_configuration
            End Get
        End Property


        ' Fields
        Private Shared s_configuration As ActiveDirectoryRoleFactoryConfiguration = DirectCast(ConfigurationManager.GetSection(ActiveDirectoryRoleFactory.s_configurationSectionName), ActiveDirectoryRoleFactoryConfiguration)
        Private Shared s_configurationSectionName As String = "System.Workflow.Runtime.Hosting.ADRoleFactory"
        Private Shared s_directoryGroupQuery As DirectoryGroupQuery = New DirectoryGroupQuery
        Private Shared s_rootEntry As DirectoryEntry
    End Class
End Namespace

